package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpSecretKeysAddDTO;
import org.jsola.hr.dto.EmpSecretKeysUpdateDTO;
import org.jsola.hr.query.EmpSecretKeysQuery;
import org.jsola.hr.service.IEmpSecretKeysService;
import org.jsola.hr.vo.EmpSecretKeysListVO;
import org.jsola.hr.vo.EmpSecretKeysVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrEmpSecretKeysController")
@RequestMapping(HrConstants.API_PATH + "/v1/empSecretKeys")
public class EmpSecretKeysController implements IEmpSecretKeysControllerAPI {

    @Autowired
    private IEmpSecretKeysService empSecretKeysService;

    @Override
    @PostMapping
    public Result<EmpSecretKeysVO> save(@Valid @RequestBody EmpSecretKeysAddDTO empSecretKeysAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工秘钥KEYS权限校验

        return Result.success(
                empSecretKeysService.save(empSecretKeysAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpSecretKeysUpdateDTO empSecretKeysUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工秘钥KEYS权限校验

        int count = empSecretKeysService.update(empSecretKeysUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{empSecretKeysId}")
    public Result<Integer> delete(@PathVariable String empSecretKeysId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工秘钥KEYS权限校验

        int count = empSecretKeysService.deleteByIds(tokenUser, empSecretKeysId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empSecretKeysIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工秘钥KEYS权限校验

        int count = empSecretKeysService.deleteByIds(tokenUser, empSecretKeysIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empSecretKeysId}")
    public Result<EmpSecretKeysVO> findById(@PathVariable String empSecretKeysId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工秘钥KEYS权限校验

        return Result.success(
            empSecretKeysService.selectById(empSecretKeysId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpSecretKeysListVO>> listAll(@Valid EmpSecretKeysQuery empSecretKeysQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工秘钥KEYS权限校验

        return Result.success(
            empSecretKeysService.select(empSecretKeysQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpSecretKeysListVO>> page(@Valid EmpSecretKeysQuery empSecretKeysQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empSecretKeysService.selectPage(empSecretKeysQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @PostMapping(value = "generateEmpRSA")
    public void generateEmpRSA(@Valid @RequestBody EmpSecretKeysUpdateDTO empSecretKeysUpdateDTO, HttpServletResponse response) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工秘钥KEYS权限校验

        try {
            String priKey = empSecretKeysService.generateEmpRSA(empSecretKeysUpdateDTO, tokenUser);
//            byte[] privateKeyBytes = empSecretKeysVO.getKeyStr().getBytes();
            response.setHeader("Content-disposition", "attachment;filename=" + new String("秘钥".getBytes("utf-8"), "iso8859-1") + ".txt");
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.getWriter().write(priKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

